package com.comdo.newup_loan.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.comdo.newup_loan.R;
import com.comdo.newup_loan.model.FlowListBean;
import com.comdo.newup_loan.util.StringUtils;

import java.util.ArrayList;

/**
 * Created by liyuan on 2017/9/27.
 */

public class TradeFlowAdapter extends RecyclerView.Adapter<TradeFlowAdapter.ViewHolder> implements View.OnClickListener {

    public ArrayList<FlowListBean.FlowBean> datas = null;
    private OnItemClickListener mOnItemClickListener = null;
    private Context context;

    public TradeFlowAdapter(ArrayList<FlowListBean.FlowBean> datas, Context context) {
        this.datas = datas;
        this.context = context;
    }

    @Override
    public void onBindViewHolder(ViewHolder viewHolder, int position) {
        //类型0线上充值1还款2提现8借款11退押金13交违约金
        String typeStr="";
        String frontStr="";
        switch (datas.get(position).getTypes()){
            case 0:
                typeStr="线上充值";
                frontStr="+";
                break;
            case 1:
                typeStr="还款";
                frontStr="-";
                break;
            case 2:
                typeStr="提现";
                frontStr="-";
                break;
            case 8:
                typeStr="借款";
                frontStr="+";
                break;
            case 11:
                typeStr="退押金";
                frontStr="+";
                break;
            case 13:
                typeStr="缴违约金";
                frontStr="-";
                break;
        }
        viewHolder.itemView.setTag(position);
        viewHolder.typeTextView.setText(typeStr);
        if(frontStr.equals("+")){
            viewHolder.moneyTextView.setTextColor(context.getResources().getColor(R.color.color_green));
        }else{
            viewHolder.moneyTextView.setTextColor(context.getResources().getColor(R.color.color_red));
        }
        viewHolder.moneyTextView.setText(frontStr+ StringUtils.getMoneyString(datas.get(position).getAmount()));
        viewHolder.leftMoneyTextView.setText("余额"+StringUtils.getMoneyString(datas.get(position).getLeftAmount()));
        viewHolder.dateTextView.setText(datas.get(position).getCreateTime());
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_flow, parent, false);
        ViewHolder vh = new ViewHolder(view);
        view.setOnClickListener(this);
        return vh;
    }

    @Override
    public int getItemCount() {
        return datas.size();
    }

    @Override
    public void onClick(View v) {
        if (mOnItemClickListener != null) {
            //注意这里使用getTag方法获取position
            mOnItemClickListener.onItemClick(v, (int) v.getTag());
        }
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    //自定义的ViewHolder，持有每个Item的的所有界面元素
    public static class ViewHolder extends RecyclerView.ViewHolder {
        public TextView typeTextView;
        public TextView moneyTextView;
        public TextView leftMoneyTextView;
        public TextView dateTextView;

        public ViewHolder(View view) {
            super(view);
            typeTextView = (TextView) view.findViewById(R.id.tv_types);
            moneyTextView = (TextView) view.findViewById(R.id.tv_amount);
            leftMoneyTextView = (TextView) view.findViewById(R.id.tv_left_amount);
            dateTextView = (TextView) view.findViewById(R.id.tv_date);
        }
    }

    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }
}
